package com.lzy.service.impl;

import com.lzy.mapper.reservationMapper;
import com.lzy.pojo.reservation;
import com.lzy.service.reservationService;
import com.lzy.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class reservationServiceImpl implements reservationService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
    @Override
    public void insert(reservation reservation) {
        SqlSession sqlSession = factory.openSession();
        reservationMapper mapper = sqlSession.getMapper(reservationMapper.class);
        mapper.insert(reservation);
        sqlSession.commit();;
        sqlSession.close();
    }

    @Override
    public reservation select(String cus_name) {
        SqlSession sqlSession = factory.openSession();
        reservationMapper mapper = sqlSession.getMapper(reservationMapper.class);
        reservation reservation = mapper.select(cus_name);
        sqlSession.close();
        return reservation;
    }

    @Override
    public int selectType(String cus_name) {
        SqlSession sqlSession = factory.openSession();
        reservationMapper mapper = sqlSession.getMapper(reservationMapper.class);
        int type = mapper.selectType(cus_name);
        sqlSession.close();
        return type;
    }

    @Override
    public String selectKey(String cus_name) {
        SqlSession sqlSession = factory.openSession();
        reservationMapper mapper = sqlSession.getMapper(reservationMapper.class);
        String key = mapper.selectKey(cus_name);
        sqlSession.close();
        return key;
    }

    @Override
    public List<String> selectTypes(String cus_name) {
        SqlSession sqlSession = factory.openSession();
        reservationMapper mapper = sqlSession.getMapper(reservationMapper.class);
        List<String> types = mapper.selectTypes(cus_name);
        sqlSession.close();
        return types;
    }

    @Override
    public List<String> selectKeys(String cus_name) {
        SqlSession sqlSession = factory.openSession();
        reservationMapper mapper = sqlSession.getMapper(reservationMapper.class);
        List<String> keys = mapper.selectKeys(cus_name);
        sqlSession.close();
        return keys;
    }

    @Override
    public void delete(String cus_name, String key) {
        SqlSession sqlSession = factory.openSession();
        reservationMapper mapper = sqlSession.getMapper(reservationMapper.class);
        mapper.delete(cus_name,key);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public reservation selectByNameAndNumber(String cus_name, String key) {
        SqlSession sqlSession = factory.openSession();
        reservationMapper mapper = sqlSession.getMapper(reservationMapper.class);
        reservation reservation = mapper.selectByNameAndNumber(cus_name, key);
        sqlSession.close();
        return reservation;
    }
}
